class User {
  custId = -1; //客服ID,预留备用
  custUsername = ''; //客服用户名
  custRemark = ''; //客服备注
  chatUid = ''; //聊天系统UID
  chatToken = ''; //聊天系统token和本系统token,除登录接口外,其余接口都需在header.Authorization携带
  chatWsAddr = ''; //聊天系统地址
}

interface ChatInfo {
  guid: string;
  remark: string;
  avator: string;
  nickname: string;
  isEdit: boolean;
}

const userInfo = (window.store.getValue('userInfo') as User) || new User();

export const useUserStore = defineStore('user', {
  state: () => ({
    userInfo,
    chatListStore: (window.store.getValue('chatList') || []) as ChatInfo[],
  }),
  getters: {
    token: (state) => state.userInfo.chatToken,
    chatList: (store) => store.chatListStore,
  },
  actions: {
    setUserInfo(userInfo: any) {
      this.userInfo = userInfo;
      window.store.setValue('userInfo', userInfo);
    },
    setAccount(username: string, password: string) {
      window.store.setValue('account', { username, password });
    },
    getAccount(): { username: string; password: string } {
      return window.store.getValue('account') || { username: '', password: '' };
    },
    exitLogin() {
      this.setUserInfo(new User());
    },
    pushChat(chat: ChatInfo) {
      this.chatListStore.push(chat);
      this.cacheChatList();
    },
    removeChat(index: number, deleteCount: number = 1) {
      this.chatListStore.splice(index, deleteCount);
      this.cacheChatList();
    },
    cacheChatList() {
      window.store.setValue(
        'chatList',
        JSON.parse(JSON.stringify(this.chatListStore))
      );
    },
  },
});
